package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author tang
 * @Date 2023/9/1 17:45
 */
@Slf4j
@Api(tags = "套餐相关接口")
@RequestMapping("/admin/setmeal")
@RestController
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    //key的规则：setmeal::分类id
    @CacheEvict(cacheNames = "setmeal", key = "#setmealDTO.categoryId")
    @ApiOperation("新增套餐")
    @PostMapping
    public Result save(@RequestBody SetmealDTO setmealDTO){
        log.info("新增套餐：{}", setmealDTO);
        setmealService.save(setmealDTO);
        return Result.success();
    }

    /**
     * 套餐分页查询
     * @param pageQueryDTO
     * @return
     */
    @ApiOperation("套餐分页查询")
    @GetMapping("/page")
    public Result page(SetmealPageQueryDTO pageQueryDTO){
        log.info("套餐分页查询: pageQueryDTO={}", pageQueryDTO);
        PageResult pageResult = setmealService.page(pageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    //key的规则：暴力清除所有setmeal下面的缓存数据  allEntries = true
    @CacheEvict(cacheNames = "setmeal", allEntries = true)
    @ApiOperation("删除套餐")
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids){
        log.info("删除套餐：ids = {}", ids);
        setmealService.delete(ids);
        return Result.success();
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @ApiOperation("根据id查询套餐")
    @GetMapping("/{id}")
    public Result<SetmealVO> getById(@PathVariable Long id){
        log.info("根据id查询套餐: id={}", id);
        SetmealVO setmealVO = setmealService.getById(id);
        return Result.success(setmealVO);
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    //key的规则：暴力清除所有setmeal下面的缓存数据  allEntries = true
    @CacheEvict(cacheNames = "setmeal", allEntries = true)
    @ApiOperation("修改套餐")
    @PutMapping
    public Result update(@RequestBody SetmealDTO setmealDTO){
        log.info("修改套餐：setmealDTO = {}", setmealDTO);
        setmealService.update(setmealDTO);
        return Result.success();
    }

    /**
     * 套餐起售、停售
     * @param status
     * @return
     */
    //key的规则：暴力清除所有setmeal下面的缓存数据  allEntries = true
    @CacheEvict(cacheNames = "setmeal", allEntries = true)
    @ApiOperation("套餐起售、停售")
    @PostMapping("/status/{status}")
    public Result enableOrDisable(@PathVariable Integer status, Long id){
        log.info("套餐起售、停售: status = {}, id = {}", status, id);
        setmealService.enableOrDisable(status, id);
        return Result.success();
    }
}
